package com.jxl.charactersheet.collections
{
	import com.jxl.charactersheet.events.InventoryChangeEvent;
	import com.jxl.charactersheet.vo.inventory.WearableItem;
	import com.jxl.dnd.equipment.Item;
	
	import mx.collections.ArrayCollection;

	[Event(name="inventoryChanged", type="com.jxl.charactersheet.events.InventoryChangeEvent")]
	public class InventoryCollection extends ArrayCollection
	{
		
		protected var __head:WearableItem;
		protected var __eyes:WearableItem;
		protected var __neck:WearableItem;
		protected var __shoulders:WearableItem;
		protected var __ringLeft:WearableItem;
		protected var __ringRight:WearableItem;
		protected var __hands:WearableItem;
		protected var __arms:WearableItem;
		protected var __body:WearableItem;
		protected var __torso:WearableItem;
		protected var __waist:WearableItem;
		protected var __feet:WearableItem;
		
		public function get head():WearableItem { return __head; }
		public function set head(val:WearableItem):void
		{
			var old:WearableItem = __head;
			__head = val;
			changeEvent(old, __head, WearableItem.HEAD);
		}
		
		public function get eyes():WearableItem { return __eyes; }
		public function set eyes(val:WearableItem):void
		{
			var old:WearableItem = __eyes;
			__eyes = val;
			changeEvent(old, __eyes, WearableItem.EYES);
		}
		
		public function get neck():WearableItem { return __neck; }
		public function set neck(val:WearableItem):void
		{
			var old:WearableItem = __neck;
			__neck = val;
			changeEvent(old, __neck, WearableItem.NECK);
		}
		
		public function get shoulders():WearableItem { return __shoulders; }
		public function set shoulders(val:WearableItem):void
		{
			var old:WearableItem = __shoulders;
			__shoulders = val;
			changeEvent(old, __shoulders, WearableItem.SHOULDERS);
		}
		
		public function get ringLeft():WearableItem { return __ringLeft; }
		public function set ringLeft(val:WearableItem):void
		{
			var old:WearableItem = __ringLeft;
			__ringLeft = val;
			changeEvent(old, __ringLeft, WearableItem.RING_LEFT);
		}
		
		public function get ringRight():WearableItem { return __ringRight; }
		public function set ringRight(val:WearableItem):void
		{
			var old:WearableItem = __ringRight;
			__ringRight = val;
			changeEvent(old, __ringRight, WearableItem.RING_RIGHT);
		}
		
		public function get hands():WearableItem { return __hands; }
		public function set hands(val:WearableItem):void
		{
			var old:WearableItem = __hands;
			__hands = val;
			changeEvent(old, __hands, WearableItem.HANDS);
		}
		
		public function get arms():WearableItem { return __arms; }
		public function set arms(val:WearableItem):void
		{
			var old:WearableItem = __arms;
			__arms = val;
			changeEvent(old, __arms, WearableItem.ARMS);
		}
		
		public function get body():WearableItem { return __body; }
		public function set body(val:WearableItem):void
		{
			var old:WearableItem = __body;
			__body = val;
			changeEvent(old, __body, WearableItem.BODY);
		}
		
		public function get torso():WearableItem { return __torso; }
		public function set torso(val:WearableItem):void
		{
			var old:WearableItem = __torso;
			__torso = val;
			changeEvent(old, __torso, WearableItem.TORSO);
		}
		
		public function get waist():WearableItem { return __waist; }
		public function set waist(val:WearableItem):void
		{
			var old:WearableItem = __waist;
			__waist = val;
			changeEvent(old, __waist, WearableItem.WAIST);
		}
		
		public function get feet():WearableItem { return __feet; }
		public function set feet(val:WearableItem):void
		{
			var old:WearableItem = __feet;
			__feet = val;
			changeEvent(old, __feet, WearableItem.FEET);
		}
		
		
		public function InventoryCollection(source:Array=null)
		{
			super(source);
		}
		
		public function removeWearableItem(item:WearableItem):void
		{
			switch(item)
			{
				case __head:					head = null;		break;
				
				case __eyes:					eyes = null;		break;
				
				case __neck:					neck = null;		break;
				
				case __shoulders:				shoulders = null;	break;
				
				case __ringLeft:				ringLeft = null;	break;
				
				case __ringRight:				ringRight = null;	break;
				
				case __hands:					hands = null;		break;
				
				case __arms:					arms = null;		break;
				
				case __body:					body = null;		break;
				
				case __torso:					torso = null;		break;
				
				case __waist:					waist = null;		break;
				
				case __feet:					feet = null;		break;
			}
		}
		
		protected function changeEvent(oldValue:WearableItem, newValue:WearableItem, type:uint):void
		{
			//if(oldValue != null)
			//{
				// put it back in the collection
			//	this.addItem(oldValue);
			//}
			
			//if(newValue != null)
			//{
				// remove it from the collection
			//	this.removeItemAt(this.getItemIndex(newValue));
			//}
			var evt:InventoryChangeEvent = new InventoryChangeEvent(InventoryChangeEvent.INVENTORY_CHANGED);
			evt.items = [newValue];
			evt.wearableType = type;
			dispatchEvent(evt);
		}
		
		public function toXML():XML
		{
			var inventoryXML:XML = <equipment />;
			var len:int = this.length;
			var item:Item;
			for(var i:uint=0; i<len; i++)
			{
				item = this.getItemAt(i) as Item;
				inventoryXML.appendChild(item.toXML());
			}
			
			if(head != null) inventoryXML.appendChild(<head>{head.toXML()}</head>);
			if(eyes != null) inventoryXML.appendChild(<eyes>{eyes.toXML()}</eyes>);
			if(neck != null) inventoryXML.appendChild(<neck>{neck.toXML()}</neck>);
			if(shoulders != null) inventoryXML.appendChild(<shoulders>{shoulders.toXML()}</shoulders>);
			if(ringLeft != null) inventoryXML.appendChild(<ringleft>{ringLeft.toXML()}</ringleft>);
			if(ringRight != null) inventoryXML.appendChild(<ringright>{ringRight.toXML()}</ringright>);
			if(hands != null) inventoryXML.appendChild(<hands>{hands.toXML()}</hands>);
			if(arms != null) inventoryXML.appendChild(<arms>{arms.toXML()}</arms>);
			if(body != null) inventoryXML.appendChild(<body>{body.toXML()}</body>);
			if(torso != null) inventoryXML.appendChild(<torso>{torso.toXML()}</torso>);
			if(waist != null) inventoryXML.appendChild(<waist>{waist.toXML()}</waist>);
			if(feet != null) inventoryXML.appendChild(<feet>{feet.toXML()}</feet>);
			
			return inventoryXML;
		}
		
		public function buildFromXML(xml:XML):void
		{
			// HACK: This class "knows" what goes in it.  Not very flexible for rebuilding itself
			var item:Item;
			var wearableItem:WearableItem;
			var list:Array = [];
			for each(var itemNode:XML in xml.item)
			{
				if(String(itemNode.@type).toLowerCase() == "item")
				{
					item = new Item();
					item.buildFromXML(itemNode);
					list.push(item);
				}
				else if(String(itemNode.@type).toLowerCase() == "wearableitem")
				{
					wearableItem = new WearableItem();
					wearableItem.buildFromXML(itemNode);
					list.push(wearableItem);
				}
			}
			
			var parts:Array = 
			[
				"head",
				"eyes",
				"neck",
				"shoulders",
				"ringLeft",
				"ringRight",
				"hands",
				"arms",
				"body",
				"torso",
				"waist",
				"feet"
			];
			var len:int = parts.length;
			var partName:String;
			var xmlItemNode:XML;
			for(var partIndex:uint = 0; partIndex < len; partIndex++)
			{
				partName = parts[partIndex] as String;
				if(xml[partName.toLowerCase()] != undefined)
				{
					this[partName] = new WearableItem();
					xmlItemNode = new XML(xml[partName.toLowerCase()]);
					WearableItem(this[partName]).buildFromXML(new XML(xmlItemNode..item[0]));
				}
			}
			
			this.source = list;
		}
		
	}
}